<?php

include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/data/Person.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/data/PersonSectorInvolvement.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/data/view/PersonSectorInvolvementFormData.php';

/**
 * Description of PersonSectorInvolvementFormConverter
 *
 * @author Reinowin7
 */
class PersonSectorInvolvementFormConverter 
{
    private $personID;
    private $name;
    private $arrayOfSelectedSectorIDs;
    private $agreesToTerms;
    private $listOfAllSectors;
    public $formatErrors;
    const HASMHAP_SIZE_ZERO_INDICATING_THAT_THERE_WERE_NO_ERRORS = 0;
    private $isDataAfterFormattingOK = false;

    public function __construct()
    {

    }

    public function convertToModelData($request)
    {
        $personSectorInvolvementFormData = null;
        $person = null;
        $formatErrors = $this->getFormatErrors();
        $this->setIsDataAfterFormattingOK(false);

        try 
        {
            $personSectorInvolvementFormData = new PersonSectorInvolvementFormData();
            $person = new Person();
            $personSectorInvolvementFormData->setPerson($person);

            try 
            {
                $personID = $this->getPersonID();
                $person->setPersonID($personID);
                $_SESSION["personID"] = $this->getPersonID();
            } 
            catch (Exception $e) 
            {

            }

            try 
            {
                if (strlen(trim($this->getName())) > 0)
                {
                    $person->setName($this->getName());
                    $_SESSION["name"] = $this->getName();
                }
                else
                {
                    $_SESSION["name"] = "";
                    throw new Exception();
                }
            }
            catch (Exception $e) 
            {
                $this->formatErrors["name"] = "There has to be a name inserted!";
            }

            try 
            {
                if (!is_null($this->getArrayOfSelectedSectorIDs()) and count($this->getArrayOfSelectedSectorIDs()) > 0)
                {
                    $_SESSION["sectorSelection[]"] = $this->getArrayOfSelectedSectorIDs();
                    $listOfPersonSectorInvolvements = array();

                    for ($i = 0; $i < count($this->getArrayOfSelectedSectorIDs()); $i++)
                    {
                        $personSectorInvolvement = new PersonSectorInvolvement();
                        $sectorID = $this->arrayOfSelectedSectorIDs[$i];
                        $personSectorInvolvement->setSectorFK($sectorID);
                        try 
                        {
                            $personID = $this->getPersonID();
                            $personSectorInvolvement->setPersonFK($personID);
                        } 
                        catch (Exception $e) 
                        {

                        }

                        if (!is_null($this->getAgreesToTerms()) and $this->getAgreesToTerms() == "1"){$personSectorInvolvement->setAgreesToTerms(true);}
                        else {$personSectorInvolvement->setAgreesToTerms(false);}

                        $listOfPersonSectorInvolvements[] = $personSectorInvolvement;//array_push($listOfPersonSectorInvolvements, $personSectorInvolvement);
                    }

                    $personSectorInvolvementFormData->setListOfPersonSectorInvolvements($listOfPersonSectorInvolvements);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception $e) 
            {
                $this->formatErrors["sectorSelection"] = "At least one selection has to be made!";
            }

            $_SESSION["agreeToTermsCheckbox"] = $this->getAgreesToTerms();

            if (count($this->getFormatErrors()) == self::HASMHAP_SIZE_ZERO_INDICATING_THAT_THERE_WERE_NO_ERRORS)
            {
                $this->setIsDataAfterFormattingOK(true);
            }
        }
        catch (Exception $e) 
        {
            $e->printStackTrace();
        }

        return $personSectorInvolvementFormData;
    }

    public function fillWithSessionDataIfThereIsAny(
            $name, array $arrayOfSelectedSectorIDs, $agreesToTerms,
            array $listOfAllSectors)
    {
        $this->setName($name);
        $this->setArrayOfSelectedSectorIDs($arrayOfSelectedSectorIDs);
        $this->setAgreesToTerms($agreesToTerms);
        $this->setListOfAllSectors($listOfAllSectors);
    }

    public function getErrorByField($fieldToGetInformationAbout) 
    {
        $fieldError = "";
        $formatErrorExemplar;

        if (!is_null($this->getFormatErrors()) and count($this->getFormatErrors()) > 0)
        {
            if (isset($this->formatErrors[$fieldToGetInformationAbout]))
            {
                $formatErrorExemplar = $this->formatErrors[$fieldToGetInformationAbout];
                if (!is_null($formatErrorExemplar))
                {
                    $fieldError = $formatErrorExemplar;
                }
            }
        }

        return $fieldError;
    }

    public function getPersonID() 
    {
        return $this->personID;
    }

    public function setPersonID($personID) 
    {
        $this->personID = $personID;
    }

    public function getName() 
    {
        return $this->name;
    }

    public function setName($name) 
    {
        $this->name = $name;
    }

    public function getArrayOfSelectedSectorIDs()
    {
        return $this->arrayOfSelectedSectorIDs;
    }

    public function setArrayOfSelectedSectorIDs(array $arrayOfSelectedSectorIDs)
    {
        $this->arrayOfSelectedSectorIDs = $arrayOfSelectedSectorIDs;
    }

    public function getAgreesToTerms() 
    {
        return $this->agreesToTerms;
    }

    public function setAgreesToTerms($agreesToTerms) 
    {
        $this->agreesToTerms = $agreesToTerms;
    }


    public function getListOfAllSectors() 
    {
        return $this->listOfAllSectors;
    }

    public function setListOfAllSectors(array $listOfAllSectors) 
    {
        $this->listOfAllSectors = $listOfAllSectors;
    }

    public function getFormatErrors() 
    {
        if (is_null($this->formatErrors))
        {
            $this->formatErrors = array();
            return $this->formatErrors;
        }
        else
        {
            return $this->formatErrors;
        }
    }

    public function setFormatErrors(array $formatErrors) 
    {
        $this->formatErrors = $formatErrors;
    }

    public function getIsDataAfterFormattingOK() 
    {
        return $this->isDataAfterFormattingOK;
    }

    public function setIsDataAfterFormattingOK($isDataAfterFormattingOK) 
    {
        $this->isDataAfterFormattingOK = $isDataAfterFormattingOK;
    }
}

?>
